package com.leejxx.health.controller;

import com.leejxx.health.common.MessageConst;
import com.leejxx.health.entity.PageResult;
import com.leejxx.health.entity.QueryPageBean;
import com.leejxx.health.entity.Result;
import com.leejxx.health.pojo.Menu;
import com.leejxx.health.service.MenuService;
import com.leejxx.health.vo.AddOrEditMenuParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author LeeJx
 * @ClassName MenuController
 * @Description TODO
 * @date 2021/1/11 11:26
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/menu")
public class MenuController {

    @DubboReference
    private MenuService menuService;

    @RequestMapping("/findByPage")
    public Result findByPage(@RequestBody QueryPageBean queryPageBean) {
        log.info("[菜单管理-分页查询]data:{}", queryPageBean);
        PageResult pageResult = menuService.findByPage(queryPageBean);
        log.info("[菜单管理-分页查询成功]data:{}", pageResult);
        return new Result(true, MessageConst.ACTION_SUCCESS, pageResult);
    }

    @RequestMapping("/findChildMenu")
    public Result findChildMenu() {
        List<Menu> childMenus= menuService.findChildMenu();
        log.info("[菜单管理-查询子菜单成功]data:{}", childMenus);
        return new Result(true, MessageConst.ACTION_SUCCESS,childMenus);
    }

    @RequestMapping("/addParentMenu")
    public Result addParentMenu(@RequestBody AddOrEditMenuParam menuParam) {
        log.info("[菜单管理-新增父菜单]data:{}", menuParam);
        Menu menu = menuParam.getMenu();
        List<Integer> menuIds = menuParam.getMenuIds();
        menuService.addParentMenu(menu, menuIds);
        log.info("[菜单管理-新增父菜单成功]");
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @RequestMapping("/addChildMenu")
    public Result addChildMenu(@RequestBody Menu menu) {
        log.info("[菜单管理-新增子菜单]data:{}", menu);
        menuService.addChildMenu(menu);
        log.info("[菜单管理-新增子菜单成功]");
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @RequestMapping("/findParentMenuById")
    public Result findParentMenuById(Integer menuId) {
        log.info("[菜单管理-编辑父菜单回显]data:{}", menuId);
        Menu menu = menuService.findById(menuId);
        List<Integer> childIds = menuService.findChildIds(menuId);

        AddOrEditMenuParam menuParam = new AddOrEditMenuParam();
        menuParam.setMenu(menu);
        menuParam.setMenuIds(childIds);

        log.info("[菜单管理-编辑父菜单回显成功]data:{}",menuParam);
        return new Result(true, MessageConst.ACTION_SUCCESS, menuParam);
    }

    @RequestMapping("/findAllChildMenu")
    public Result findAllChildMenu() {
        List<Menu> childMenus = menuService.findAllChildMenu();
        log.info("[菜单管理-编辑父菜单子菜单回显成功]data:{}",childMenus);
        return new Result(true, MessageConst.ACTION_SUCCESS, childMenus);
    }

    @RequestMapping("/edit4Parent")
    public Result edit4Parent(@RequestBody AddOrEditMenuParam menuParam) {
        log.info("[菜单管理-编辑父菜单]data:{}", menuParam);
        Menu menu = menuParam.getMenu();
        List<Integer> menuIds = menuParam.getMenuIds();
        menuService.editParentMenu(menu, menuIds);
        log.info("[菜单管理-编辑父菜单成功]");
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @RequestMapping("/edit4Child")
    public Result edit4Child(@RequestBody Menu menu) {
        log.info("[菜单管理-编辑子菜单]data:{}", menu);
        menuService.editChildMenu(menu);
        log.info("[菜单管理-编辑子菜单成功]");
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @RequestMapping("/findChildMenuById")
    public Result findChildMenuById(Integer menuId) {
        log.info("[菜单管理-编辑子菜单回显]data:{}", menuId);
        Menu menu = menuService.findById(menuId);

        log.info("[菜单管理-编辑子菜单回显成功]data:{}",menu);
        return new Result(true, MessageConst.ACTION_SUCCESS, menu);
    }

    @RequestMapping("/delete")
    public Result delete(Integer menuId) {
        log.info("[菜单管理-删除菜单]data:{}", menuId);
        menuService.delete(menuId);

        log.info("[菜单管理-删除菜单成功]");
        return new Result(true, MessageConst.ACTION_SUCCESS);
    }

    @RequestMapping("/getMenuList")
    public Result getMenuList() {
        List<Map<String, Object>> menuList = new ArrayList<>();
        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (null != principal && principal instanceof User) {
            User user = (User) principal;
            menuList = menuService.getMenuList(user.getUsername());
            log.info("[菜单管理-获取动态菜单信息]data:{}", menuList);
            return new Result(true, MessageConst.GET_MENU_SUCCESS, menuList);
        }
        return new Result(false, MessageConst.GET_MENU_FAIL);
    }
}
